package com.bang.low.search;

import com.alibaba.fastjson.JSONObject;
import com.bang.EsUtils;
import org.apache.http.StatusLine;
import org.apache.http.entity.ContentType;
import org.apache.http.nio.entity.NStringEntity;
import org.apache.http.util.EntityUtils;
import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
import org.elasticsearch.action.admin.indices.get.GetIndexResponse;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.client.*;

import java.io.IOException;

/**
 * @ClassName: SearchApi
 * @Desc: TODO
 * @author: <Bang>
 * @Date: 2022/01/19 14:42
 * @Version: 1.0
 * @Modified By:
 */
public class SearchApi {

    public static void main(String[] args) throws IOException {
        search("/abc/_search", new JSONObject());
        indices("/_cat/indices?h=index,health", new JSONObject());
    }

    public static void search(String endpoint, JSONObject json) throws IOException {
        final RestClient lowLevelClient = EsUtils.getLowLevelClient();
        final Request request = new Request("POST", endpoint);
        request.setEntity(new NStringEntity(json.toJSONString(), ContentType.APPLICATION_JSON));
        try {
            final Response response = lowLevelClient.performRequest(request);
            final StatusLine statusLine = response.getStatusLine();
            final int statusCode = statusLine.getStatusCode();
            String responseBody = EntityUtils.toString(response.getEntity());
            System.out.println("statusCode:" + statusCode);
            System.out.println("response:" + responseBody);


        } catch (ResponseException e) {
            final Response response = e.getResponse();
            final StatusLine statusLine = response.getStatusLine();
            String responseBody = EntityUtils.toString(response.getEntity());
            System.out.println("statusCode:" + statusLine.getStatusCode());
            System.out.println("response:" + responseBody);
        }
    }

    public static void indices(String endpoint, JSONObject json) throws IOException {
        final RestClient lowLevelClient = EsUtils.getLowLevelClient();
        final Request request = new Request("GET", endpoint);
        final RequestOptions.Builder options = RequestOptions.DEFAULT.toBuilder();
        options.addHeader("Accept", "application/json");
        request.setOptions(options);
        final NStringEntity entity = new NStringEntity(json.toJSONString(), ContentType.APPLICATION_JSON);
        request.setEntity(entity);
        try {
            final Response response = lowLevelClient.performRequest(request);
            final StatusLine statusLine = response.getStatusLine();
            final int statusCode = statusLine.getStatusCode();
            String responseBody = EntityUtils.toString(response.getEntity());
            System.out.println("statusCode:" + statusCode);
            System.out.println("response:" + responseBody);


        } catch (ResponseException e) {
            final Response response = e.getResponse();
            final StatusLine statusLine = response.getStatusLine();
            String responseBody = EntityUtils.toString(response.getEntity());
            System.out.println("statusCode:" + statusLine.getStatusCode());
            System.out.println("response:" + responseBody);
        }
    }


}
